from fastapi import Request
from pydantic import BaseModel, field_validator
from sqlalchemy.ext.asyncio import AsyncSession

from apps.system.cruds.user import UserDal
from apps.system.schemas.user import UserOut
from utils.validator import vali_telephone


class LoginForm(BaseModel):
    username: str
    password: str


class LoginResult(BaseModel):
    status: bool | None = False
    user: UserOut | None = None
    msg: str | None = None

    class Config:
        arbitrary_types_allowed = True


class LoginValidation:
    """
    验证用户登录时提交的数据是否有效
    """

    def __init__(self, func):
        self.func = func

    async def __call__(self, data: LoginForm, db: AsyncSession, request: Request) -> LoginResult:
        self.result = LoginResult()
        user = await UserDal(db).get_data(username=data.username, v_return_none=True)
        if not user:
            self.result.msg = "该账号不存在！"
            return self.result

        return self.result
